package com.task.core.service.impl;

import com.task.common.annotation.DataScope;
import com.task.common.constant.ErrorCodeConstants;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.core.domain.DataScriptTypeInfo;
import com.task.core.dto.request.DataScriptTypeRequestDTO;
import com.task.core.mapper.DataScriptTypeMapper;
import com.task.core.service.DataScriptTypeService;
import com.task.core.vo.resp.DataScriptTypeDetailRespVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 脚本类型管理(DataScriptType)表服务实现类
 *
 * @author makejava
 * @since 2025-02-26 10:31:48
 */
@Service("dataScriptTypeService")
public class DataScriptTypeServiceImpl implements DataScriptTypeService {

    @Resource
    private DataScriptTypeMapper dataScriptTypeMapper;

    /**
     * 新增脚本类型
     *
     * @param request
     * @return
     */
    @Override
    public void addScriptType(DataScriptTypeRequestDTO request) {
        //1.参数校验
        verificationParam(request);

        //2.插入数据
        request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
        request.setCode(UUID.randomUUID().toString().replace("-", ""));
        dataScriptTypeMapper.addScriptType(request);
    }

    /**
     * 获取脚本类型选项
     *
     * @param request
     * @return
     */
    @Override
    @DataScope(deptAlias = "dept", userAlias = "su")
    public List<KeyAndValue> getScriptTypeForOption(DataScriptTypeRequestDTO request) {
        return dataScriptTypeMapper.getScriptTypeForOption(request);
    }

    @Override
    @DataScope(deptAlias = "dept", userAlias = "su")
    public List<DataScriptTypeInfo> getScriptTypeByPage(DataScriptTypeRequestDTO request) {
        return dataScriptTypeMapper.getScriptTypeByPage(request);
    }

    @Override
    public void modifyScriptType(DataScriptTypeRequestDTO request) {
        if (StringUtils.isEmpty(request.getCode())) {
            throw new ServiceException(ErrorCodeConstants.SCRIPT_TYPE_CODE_IS_EMPTY.getMsg());
        }
        request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
        Integer integer = dataScriptTypeMapper.modifyScriptType(request);
        if(integer == 0) {
            throw new ServiceException(ErrorCodeConstants.SCRIPT_TYPE_CODE_IS_EMPTY.getMsg());
        }
    }

    @Override
    public void deleteScriptType(String scriptTypeCode, Long userId) {
        Integer integer = dataScriptTypeMapper.deleteScriptType(scriptTypeCode, userId);
        if(integer == 0) {
            throw new ServiceException(ErrorCodeConstants.SCRIPT_TYPE_CODE_IS_EMPTY.getMsg());
        }
    }

    @Override
    public DataScriptTypeInfo getScriptTypeCode(String fileTypeCode) {
        DataScriptTypeInfo  dataScriptTypeInfo= dataScriptTypeMapper.getScriptTypeCode(fileTypeCode);
        return dataScriptTypeInfo;
    }

    @Override
    @DataScope(deptAlias = "dept",userAlias = "su")
    public DataScriptTypeDetailRespVo getScriptTypeDetail(String scriptTypeCode) {
        DataScriptTypeRequestDTO dataScriptTypeRequestDTO = new DataScriptTypeRequestDTO();
        dataScriptTypeRequestDTO.setCode(scriptTypeCode);
        return dataScriptTypeMapper.getScriptTypeDetail(dataScriptTypeRequestDTO);
    }

    private void verificationParam(DataScriptTypeRequestDTO request) {
        if(ObjectUtils.isEmpty(request.getScriptType())){
            throw new ServiceException(ErrorCodeConstants.SCRIPT_TYPE_IS_NULL.getMsg());
        }
    }
}
